import { Component, OnInit, ViewChild } from '@angular/core';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { NzModalService } from 'ng-zorro-antd/modal';
import { PartnerAccountManagementVirtualAccountDetailComponent } from 'src/app/routes/partner/account-management/components/virtual-account-detail/virtual-account-detail.component';
import { AccountManagemantService } from 'src/app/routes/partner/account-management/services/account-managemant.service';
import { PartnerEditComponent } from '../edit/edit.component';

@Component({
  selector: 'app-partner-list',
  templateUrl: './list.component.html',
})
export class PartnerKnowledgeClassificationListComponent implements OnInit {
  url = `/user`;
  schema!: SFSchema;
  ui!: SFUISchema;
  @ViewChild('st') private readonly st!: STComponent;
  @ViewChild('sf') private readonly sf!: SFComponent;
  columns: STColumn[] = [];

  constructor(public service: AccountManagemantService, public modal: NzModalService) { }
  /**
   * 查询参数
   */
  get reqParams() {
    const params = { ...this.sf?.value };
    return params
  }

  ngOnInit(): void {
    this.initSF();
    this.initST();
  }

  initSF() {
    this.schema = {
      properties: {
        abnormalCause: {
          title: '分类ID',
          type: 'string',
          ui: {
            placeholder: '请输入',
          },
        },
        abnormalCause1: {
          title: '分类名称',
          type: 'string',
          ui: {
            placeholder: '请输入',
          },
        },
        abnormalCause2: {
          title: '状态',
          type: 'string',
          enum: [
            { label: '全部', value: '' },
            { label: '正常', value: '1' },
            { label: '禁用', value: '2' }
          ],
          default: '',
        },
      }
    }
    this.ui = { '*': { spanLabelFixed: 90, grid: { span: 8, gutter: 4 } }, };
  }
  /**
 * 初始化数据列表
 */
  initST() {
    this.columns = [
      { title: '分类ID', index: 'carNo', className: 'text-center', width: 150 },
      { title: '分类名称', render: 'carModelLabel', className: 'text-center', width: 200 },
      { title: '图标', render: 'icon', className: 'text-center', width: 200 },
      { title: '文章数', render: 'approvalStatus2', className: 'text-center', width: 120 },
      { title: '排序', render: 'approvalStatus3', className: 'text-center', width: 120 },
      { title: '状态', index: 'approvalStatus4', className: 'text-center', width: 120 },
      { title: '最后修改时间', index: 'approvalStatus4', className: 'text-center', width: 180 },
      {
        title: '操作',
        width: 150,
        buttons: [
          {
            text: '修改',
            click: (_record) => this.edit(_record)
          },
          {
            text: '启用',
            click: (_record) => this.operate(_record, 1)
          },
          {
            text: '禁用',
            click: (_record) => this.operate(_record, 2)
          }
        ]
      }
    ];
  }

  resetSF() {
    this.sf.reset();
    setTimeout(() => {
      this.st.reset();
    })
  }

  /**
   *新增
   * @param _record 当前行信息
   */
  add() {
    const modalRef = this.modal.create({
      nzTitle: '新增分类',
      nzContent: PartnerEditComponent,
      nzWidth: 600,
      nzComponentParams: {
        i: null
      },
      nzFooter: null
    });
    modalRef.afterClose.subscribe((res: any) => {
      if (res) {
        this.st.load(1);
      }
    });
  }

  /**
   *编辑
   * @param _record 当前行信息
   */
  edit(record: any) {
    const modalRef = this.modal.create({
      nzTitle: '修改分类',
      nzContent: PartnerEditComponent,
      nzWidth: 600,
      nzComponentParams: {
        i: record
      },
      nzFooter: null
    });
    modalRef.afterClose.subscribe((res: any) => {
      if (res) {
        this.st.load(1);
      }
    });
  }

  /**
 *禁用或者启动
 * @param _record 当前行信息
 */
  operate(record: any, type = 1) {
    this.modal.confirm({
      nzTitle: `<b>确定${type === 1 ? '启用' : '禁用'}此分类吗？</b>`,
      nzOnOk: () =>
        this.service.request(this.service.$api_edit_one, { id: record.id }).subscribe((res) => {
          if (res) {
            this.st.load(1);
            this.service.msgSrv.success(`${type === 1 ? '启用' : '禁用'}成功！`);
          }
        }),
    });
  }

  /**
   *
   */
  export() {

  }

}
